package com.deer.wms.company.web;


import com.deer.wms.company.model.Company;
import com.deer.wms.company.model.CompanyCriteria;
import com.deer.wms.company.model.CompanyVo;
import com.deer.wms.company.service.CompanyService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.MD5Utils;
import com.deer.wms.project.root.util.RandomUtil;
import com.deer.wms.system.manage.model.user.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wei
 * @since 2021-07-23
 */
@Api("企业信息api接口")
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 校验公司是否已存在
     * @param name 企业名称
     */
    @ApiOperation(value = "验证公司是否已存在")
    @PostMapping("/validName")
    public Result validName(String name) {
        if (companyService.findBy("name", name) != null) {
            throw new ServiceException(CommonCode.EQUALE_EXISTS);
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 租户管理员角色申请租户使用
     * @param companyVo companyVo
     */
    @ApiOperation(value = "新增租户", notes = "租户角色申请的时候新增租户及管理员信息，后续还需要审核")
    @PostMapping("/add")
    public Result add(@RequestBody CompanyVo companyVo) {
        //新增一个公司, 状态默认为待审核状态
        Company company = new Company();
        company.setCompanyAddress(companyVo.getCompanyAddress());
        company.setName(companyVo.getCompanyName());
        company.setManagerId(companyVo.getAdminIphone());

        //新增一个用户, 特殊状态
        UserInfo userInfo = new UserInfo();
        userInfo.setAccount(companyVo.getAdminIphone());
        userInfo.setUserName(companyVo.getAdminName());
        String salt = RandomUtil.generateString(10);
        userInfo.setPassword(MD5Utils.encryptPassword(userInfo.getAccount(), companyVo.getAdminPassword(), salt));
        userInfo.setSalt(salt);
        userInfo.setCreateUserId(-1);
        userInfo.setUrl("/homepage");
        // 这里的状态设置为无效
        userInfo.setState("invalid");

        companyService.add(company, userInfo);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 查询已审核通过租户
     * @param criteria criteria
     */
    @ApiOperation(value = "查询非待审核所有租户", notes = "查询所有用于页面table展示")
    @GetMapping("/listAll")
    public Result queryAll(CompanyCriteria criteria) {
        List<Company> list = companyService.selectCheckedCompanies(criteria);
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * 查询所有待审核租户
     * @param criteria criteria
     */
    @ApiOperation(value = "查询所有待审核租户", notes = "查询所有待审核租户用于页面table展示")
    @GetMapping("/listNew")
    public Result queryNew(CompanyCriteria criteria) {
        List<Company> list = companyService.selectNewCompanies(criteria);
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * 根据id查询单个租户信息
     * @param id 租户id
     */
    @ApiOperation(value = "查询单条", notes = "选择单条展示单条公司的信息")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Integer id) {

        //TODO:
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 审核
     * @param companyId 租户id
     * @param result 0:不通过， 1: 通过
     */
    @ApiOperation(value = "审核", notes = "审核租户，通过或者不通过")
    @GetMapping("/verify")
    public Result verify(String companyId, Integer result) {
        Company company = companyService.getById(companyId);
        //result 2: 不通过,  1: 通过   0: 初始状态
        if (result == 2) {
            //标识company的状态  不通过
            company.setAuditState(2);
            companyService.save(company);
        } else if (result == 1) {
            //修改租户状态
            //激活用户
            //分配角色
            companyService.pass(company);
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 租户管理员角色修改租户信息
     */
    @ApiOperation(value = "修改", notes = "saas管理员修改状态，或租户角色修改公司属性等信息")
    @PutMapping("/update")
    public Result update() {
        //TODO:
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 删除租户
     */
    @ApiOperation(value = "删除租户", notes = "saas管理员可删除")
    @DeleteMapping("/delete")
    public Result delete(){
        //TODO:
        return ResultGenerator.genSuccessResult();
    }

}

